package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BusinessOperationDeleteDto;
import com.kaili.basic.pojo.entity.WarehousingProduct;
import com.kaili.basic.service.WarehousingProductService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.basic.pojo.dto.WarehousingApplicationAddDto;
import com.kaili.basic.pojo.dto.WarehousingApplicationQueryDto;
import com.kaili.basic.pojo.dto.WarehousingApplicationUpdateDto;
import com.kaili.basic.pojo.entity.WarehousingApplication;
import com.kaili.basic.pojo.entity.WarehousingApplicationExport;
import com.kaili.basic.service.WarehousingApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 入库申请 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "入库申请", description = "入库申请相关API")
public class WarehousingApplicationController {
    @Autowired
    WarehousingApplicationService warehousingApplicationService;
    @Autowired
    WarehousingProductService warehousingProductService;

    @PostMapping(value = "/warehousingApplications/addWarehousingApplication")
    @ApiOperation(value = "新增入库申请",notes = "新增入库申请")
    public RestStatus addWarehousingApplication(@RequestBody WarehousingApplicationAddDto warehousingApplicationAddDto){
        warehousingApplicationService.addWarehousingApplication(warehousingApplicationAddDto);
        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/warehousingApplications/updateWarehousingApplication")
    @ApiOperation(value = "修改入库申请",notes = "修改入库申请")
    public RestStatus updateWarehousingApplication(@RequestBody WarehousingApplicationUpdateDto warehousingApplicationUpdateDto){
        warehousingApplicationService.updateWarehousingApplication(warehousingApplicationUpdateDto);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/warehousingApplications/deleteWarehousingApplications")
    @ApiOperation(value = "根据主键id删除记录", notes = "根据主键id删除记录")
    public RestStatus deleteWarehousingApplications(@RequestBody BusinessOperationDeleteDto deleteDto) {
        warehousingApplicationService.deleteWarehousingApplications(deleteDto);
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/warehousingApplications/queryWarehousingApplication")
    @ApiOperation(value = "条件查询入库申请信息",notes = "条件查询入库申请信息")
    public Page<WarehousingApplication> queryWarehousingApplication(@ModelAttribute WarehousingApplicationQueryDto warehousingApplicationQueryDto) {
        return warehousingApplicationService.queryWarehousingApplication(new Page<WarehousingApplication>(warehousingApplicationQueryDto.getPage(),warehousingApplicationQueryDto.getPageSize()),warehousingApplicationQueryDto);
    }

    @GetMapping(value = "/warehousingApplications/{applyId}/queryWarehousingApplicationById")
    @ApiOperation(value = "根据入库申请主键id查询入库申请信息",notes = "根据入库申请主键id查询入库申请信息")
    public WarehousingApplication queryWarehousingApplicationById(@PathVariable("applyId") Long applyId) {
        return warehousingApplicationService.queryWarehousingApplicationById(applyId);
    }

    @GetMapping(value = "/warehousingApplications/{id}/queryWarehousingProductById")
    @ApiOperation(value = "根据入库产品主键id查询入库产品信息",notes = "根据入库产品主键id查询入库产品信息")
    public WarehousingProduct queryWarehousingProductById(@PathVariable("id") Long id) {
        return warehousingProductService.getWarehousingProductById(id);
    }

    @PutMapping(value = "/warehousingApplications/{applyId}/orderConfirmation")
    @ApiOperation(value = "订单确认",notes = "订单确认")
    public RestStatus orderConfirmation(@PathVariable("applyId") Long applyId) {
        WarehousingApplication warehousingApplication = new WarehousingApplication();
        warehousingApplication.setApplyId(applyId);
        warehousingApplication.setOrderStatus(2);
        warehousingApplication.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        warehousingApplicationService.updateAffirmOrCancel(warehousingApplication);
        return CommonStatus.ORDER_CONFIRMATION;
    }

    @PutMapping(value = "/warehousingApplications/{applyId}/orderCancellation")
    @ApiOperation(value = "订单取消",notes = "订单取消")
    public RestStatus orderCancellation(@PathVariable("applyId") Long applyId) {
        WarehousingApplication warehousingApplication = new WarehousingApplication();
        warehousingApplication.setApplyId(applyId);
        warehousingApplication.setOrderStatus(3);
        warehousingApplication.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        warehousingApplicationService.updateAffirmOrCancel(warehousingApplication);
        return CommonStatus.ORDER_CANCEL;
    }

    @GetMapping(value = "/warehousingApplications/getRequestCode")
    @ApiOperation(value = "获取申请单号",notes = "获取申请单号")
    public String getRequestCode() {
        Long id = warehousingApplicationService.getMaxApplyId();
        id = id + 1L;
        return StringUtils.leftPad(id.toString(), 6, "0");
    }

    @GetMapping("/warehousingApplications/export")
    @ApiOperation(value = "导出入库申请信息")
    public void export(HttpServletResponse response, @ModelAttribute WarehousingApplicationQueryDto warehousingApplicationQueryDto) throws RestException {
        List<WarehousingApplicationExport> list = warehousingApplicationService.queryWarehousingApplicationExport(warehousingApplicationQueryDto);
        ExcelUtil.exportExcel(list,"入库申请信息","sheet1", WarehousingApplicationExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

